package com.engine4cloud.tdf.supervision.service.impl;

import com.engine4cloud.tdf.common.core.constant.enums.AllAuditActionStatusEnum;
import com.engine4cloud.tdf.common.core.constant.enums.AuditBusinessTypeEnum;
import com.engine4cloud.tdf.common.core.constant.enums.ComplexAuditActionStatusEnum;
import com.engine4cloud.tdf.common.core.exception.ValidateCodeException;
import com.engine4cloud.tdf.supervision.dto.SpAuditDTO;
import com.engine4cloud.tdf.supervision.dto.create.SpAuditResultCreateDTO;
import com.engine4cloud.tdf.supervision.dto.update.SpComplexPreActionUpdateDTO;
import com.engine4cloud.tdf.supervision.service.*;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Service
public class SpRevokeParoleAuditServiceImpl implements SpRevokeParoleAuditService {
    @Resource
    private SpAuditService auditService;
    @Resource
    private SpAuthorizeService authorizeService;
    @Resource
    private SpApproveService approveService;
    @Resource
    private SpRevokeParoleService revokeParoleService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean audit(SpAuditDTO dto) {
        Optional.ofNullable(revokeParoleService.getByBatch(dto.getLcbh()))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));

        revokeParoleService.changeAction(new SpComplexPreActionUpdateDTO(dto.getRybh(), dto.getLcbh(), ComplexAuditActionStatusEnum.VILLAGE_AUDIT));
        return auditService.villageAudit(new SpAuditResultCreateDTO(dto, AuditBusinessTypeEnum.REVOKE_PAROLE, AllAuditActionStatusEnum.VILLAGE_AUDIT));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean refuse(SpAuditDTO dto) {
        Optional.ofNullable(revokeParoleService.getByBatch(dto.getLcbh()))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));

        revokeParoleService.changeAction(new SpComplexPreActionUpdateDTO(dto.getRybh(), dto.getLcbh(), ComplexAuditActionStatusEnum.REFUSE));
        return auditService.refuse(new SpAuditResultCreateDTO(dto, AuditBusinessTypeEnum.REVOKE_PAROLE, AllAuditActionStatusEnum.REFUSE));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean authorize(SpAuditDTO dto) {
        Optional.ofNullable(revokeParoleService.getByBatch(dto.getLcbh()))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));

        revokeParoleService.changeAction(new SpComplexPreActionUpdateDTO(dto.getRybh(), dto.getLcbh(), ComplexAuditActionStatusEnum.COUNTRY_AUTHORIZE));
        return authorizeService.authorize(new SpAuditResultCreateDTO(dto, AuditBusinessTypeEnum.REVOKE_PAROLE, AllAuditActionStatusEnum.COUNTRY_AUTHORIZE));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean reject(SpAuditDTO dto) {
        Optional.ofNullable(revokeParoleService.getByBatch(dto.getLcbh()))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));

        revokeParoleService.changeAction(new SpComplexPreActionUpdateDTO(dto.getRybh(), dto.getLcbh(), ComplexAuditActionStatusEnum.REJECT));
        return authorizeService.reject(new SpAuditResultCreateDTO(dto, AuditBusinessTypeEnum.REVOKE_PAROLE, AllAuditActionStatusEnum.REJECT));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean approve(SpAuditDTO dto) {
        Optional.ofNullable(revokeParoleService.getByBatch(dto.getLcbh()))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));

        revokeParoleService.changeAction(new SpComplexPreActionUpdateDTO(dto.getRybh(), dto.getLcbh(), ComplexAuditActionStatusEnum.APPROVE));
        return approveService.approve(new SpAuditResultCreateDTO(dto, AuditBusinessTypeEnum.REVOKE_PAROLE, AllAuditActionStatusEnum.APPROVE));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean veto(SpAuditDTO dto) {
        Optional.ofNullable(revokeParoleService.getByBatch(dto.getLcbh()))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));

        revokeParoleService.changeAction(new SpComplexPreActionUpdateDTO(dto.getRybh(), dto.getLcbh(), ComplexAuditActionStatusEnum.VETO));
        return approveService.veto(new SpAuditResultCreateDTO(dto, AuditBusinessTypeEnum.REVOKE_PAROLE, AllAuditActionStatusEnum.VETO));
    }
}
